package logic

import (
	"Serve/VideoSrv"
	"Serve/global/model"
	"Serve/internal/svc"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoUpdateLogic {
	return &VideoUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoUpdateLogic) VideoUpdate(in *VideoSrv.VideoUpdateRequest) (*VideoSrv.VideoUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var video model.Video
	video.ID = uint(in.VideoId)
	if err := model.DB.Where(&video).Find(&video).Error; err != nil {
		return nil, err
	}
	if video.ID == 0 {
		return nil, fmt.Errorf("视频不存在")
	}
	if video.UserId != int(in.UserId) {
		return nil, fmt.Errorf("不可修改他人视频")
	}
	video = model.Video{
		Title:    in.Title,
		ClassId:  int(in.Class),
		Des:      in.Des,
		Tag:      in.Tag,
		ImageUrl: in.ImageUrl,
	}
	if err := model.DB.Where("id = ?", video.ID).Updates(&video).Error; err != nil {
		return nil, err
	}
	return &VideoSrv.VideoUpdateResponse{}, nil
}
